<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.jpg">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"matian.connorma.cn","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="项目打包时，直接用spring-boot maven plugin之类插件组装出一个包含了所有依赖的jar，即fat-jar的形式，可以直接启动，部署方便。但是并不是任何时候fat-jar都是最优解。 依赖文件分离打包在几种场景下的优化稳定版本依赖包的分离对部署的优化在项目开发中，依赖包我们期望是稳定的：版本的升级一定是审慎进行的，新依赖的加入也需要考量。也因此，在项目部署时，如果只打包源代码到一">
<meta property="og:type" content="article">
<meta property="og:title" content="java项目使用thin-jar分离依赖优化部署">
<meta property="og:url" content="http://matian.connorma.cn/d8ab2921.html">
<meta property="og:site_name" content="马天">
<meta property="og:description" content="项目打包时，直接用spring-boot maven plugin之类插件组装出一个包含了所有依赖的jar，即fat-jar的形式，可以直接启动，部署方便。但是并不是任何时候fat-jar都是最优解。 依赖文件分离打包在几种场景下的优化稳定版本依赖包的分离对部署的优化在项目开发中，依赖包我们期望是稳定的：版本的升级一定是审慎进行的，新依赖的加入也需要考量。也因此，在项目部署时，如果只打包源代码到一">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-03-03T00:39:56.000Z">
<meta property="article:modified_time" content="2024-01-25T12:19:17.208Z">
<meta property="article:author" content="马天">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://matian.connorma.cn/d8ab2921.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>java项目使用thin-jar分离依赖优化部署 | 马天</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">马天</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://matian.connorma.cn/d8ab2921.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="马天">
      <meta itemprop="description" content="程序猿">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="马天">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          java项目使用thin-jar分离依赖优化部署
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-03-03 08:39:56" itemprop="dateCreated datePublished" datetime="2019-03-03T08:39:56+08:00">2019-03-03</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>项目打包时，直接用spring-boot maven plugin之类插件组装出一个包含了所有依赖的jar，即fat-jar的形式，可以直接启动，部署方便。但是并不是任何时候fat-jar都是最优解。</p>
<h2 id="依赖文件分离打包在几种场景下的优化"><a href="#依赖文件分离打包在几种场景下的优化" class="headerlink" title="依赖文件分离打包在几种场景下的优化"></a>依赖文件分离打包在几种场景下的优化</h2><h3 id="稳定版本依赖包的分离对部署的优化"><a href="#稳定版本依赖包的分离对部署的优化" class="headerlink" title="稳定版本依赖包的分离对部署的优化"></a>稳定版本依赖包的分离对部署的优化</h3><p>在项目开发中，依赖包我们期望是稳定的：版本的升级一定是审慎进行的，新依赖的加入也需要考量。也因此，在项目部署时，如果只打包源代码到一个thin-jar、将固定不变依赖包分离，打包得到的部署文件体积将大大减小——使用spring boot开发的项目打包成fat-jar百兆起步，但其实源代码打包的thin-jar体积很可能是KB级的。</p>
<p>尤其是在使用docker镜像部署jar时，对于镜像打包有优化：虽然对镜像体积减小不会有优化，但是可以将依赖文件置入单独的镜像层复用，镜像构建时多数情况下（依赖未变更的情况下）只需要重新构建源代码及它之上的层，构建速度将会得到提升。</p>
<span id="more"></span>

<h3 id="不得不使用指定classpath的情况——依赖包面向不同OS-x2F-硬件架构"><a href="#不得不使用指定classpath的情况——依赖包面向不同OS-x2F-硬件架构" class="headerlink" title="不得不使用指定classpath的情况——依赖包面向不同OS&#x2F;硬件架构"></a>不得不使用指定classpath的情况——依赖包面向不同OS&#x2F;硬件架构</h3><p>这里直接举一个例子</p>
<p>当我们集成luben zstd做文件压缩时，zstd的实现需要通过jni调用so&#x2F;dll包，对不同OS&#x2F;硬件架构需要引入不同的依赖包，此时启动项目时需要对不同的OS&#x2F;硬件架构选择对应的依赖包</p>
<p>在很多开源项目中也可以看到，通过分离依赖包，可以根据部署情况替换依赖包版本。结合动态加载的机制，那么可以通过添加依赖jar扩展能力——比如hadoop扩展支持的shuffle实现、扩展支持的存储等</p>
<h3 id="另一种情况：使用classpath引入线上依赖包"><a href="#另一种情况：使用classpath引入线上依赖包" class="headerlink" title="另一种情况：使用classpath引入线上依赖包"></a>另一种情况：使用classpath引入线上依赖包</h3><p>举一个例子，在大数据平台计算集群中，通常只需要客户端提供一个shade jar，而对于必须引用的hadoop依赖，通过线上环境的启动参数指定classpath引入的，这样对平台依赖的管理和升级方便。</p>
<h2 id="依赖文件分离打包示例"><a href="#依赖文件分离打包示例" class="headerlink" title="依赖文件分离打包示例"></a>依赖文件分离打包示例</h2><p>这里给出一个简单的例子，演示如何使用maven plugin快速完成依赖分离打包和启动方式</p>
<p>maven默认的打包插件得到的是一个thin-jar，不过并没有自动的将依赖包归集到一起，可以使用如下配置来完成：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- other content in pom.xml --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- ....... --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- your other dependencies --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- ....... --&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- provided方式引入luben zstd依赖 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.luben<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zstd-jni<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.5.4-2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 在win_amd64平台开发 --&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--&lt;classifier&gt;win_amd64&lt;/classifier&gt;--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">scope</span>&gt;</span>provided<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 引入必要依赖 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">pluginManagement</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-antrun-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.1.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-dependency-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">pluginManagement</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">profiles</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">profile</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">id</span>&gt;</span>manual<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">activation</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">activeByDefault</span>&gt;</span>true<span class="tag">&lt;/<span class="name">activeByDefault</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">activation</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">                    <span class="comment">&lt;!-- 将依赖包归集到构建输出路径下lib/下 --&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-dependency-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">executions</span>&gt;</span></span><br><span class="line">                            <span class="tag">&lt;<span class="name">execution</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;<span class="name">goals</span>&gt;</span></span><br><span class="line">                                    <span class="tag">&lt;<span class="name">goal</span>&gt;</span>copy-dependencies<span class="tag">&lt;/<span class="name">goal</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;/<span class="name">goals</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;<span class="name">phase</span>&gt;</span>prepare-package<span class="tag">&lt;/<span class="name">phase</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">                                    <span class="tag">&lt;<span class="name">includeScope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">includeScope</span>&gt;</span></span><br><span class="line">                                    <span class="tag">&lt;<span class="name">outputDirectory</span>&gt;</span>$&#123;project.build.directory&#125;/lib</span><br><span class="line">                                    <span class="tag">&lt;/<span class="name">outputDirectory</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br><span class="line">                            <span class="tag">&lt;/<span class="name">execution</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;/<span class="name">executions</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">                        <span class="comment">&lt;!-- 将lib文件夹打包为tar、tar.gz、zip文件，方便部署 --&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-antrun-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">executions</span>&gt;</span></span><br><span class="line">                            <span class="tag">&lt;<span class="name">execution</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;<span class="name">id</span>&gt;</span>antrun-archive<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;<span class="name">goals</span>&gt;</span></span><br><span class="line">                                    <span class="tag">&lt;<span class="name">goal</span>&gt;</span>run<span class="tag">&lt;/<span class="name">goal</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;/<span class="name">goals</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;<span class="name">phase</span>&gt;</span>package<span class="tag">&lt;/<span class="name">phase</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">                                    <span class="tag">&lt;<span class="name">target</span>&gt;</span></span><br><span class="line">                                        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;archive.includes&quot;</span> <span class="attr">value</span>=<span class="string">&quot;lib/*&quot;</span>/&gt;</span></span><br><span class="line">                                        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;tar.destfile&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;project.build.directory&#125;/lib.tar&quot;</span>/&gt;</span></span><br><span class="line">                                        <span class="tag">&lt;<span class="name">zip</span> <span class="attr">basedir</span>=<span class="string">&quot;$&#123;project.build.directory&#125;&quot;</span></span></span><br><span class="line"><span class="tag">                                             <span class="attr">destfile</span>=<span class="string">&quot;$&#123;project.build.directory&#125;/lib.zip&quot;</span></span></span><br><span class="line"><span class="tag">                                             <span class="attr">includes</span>=<span class="string">&quot;$&#123;archive.includes&#125;&quot;</span>/&gt;</span></span><br><span class="line">                                        <span class="tag">&lt;<span class="name">tar</span> <span class="attr">basedir</span>=<span class="string">&quot;$&#123;project.build.directory&#125;&quot;</span> <span class="attr">destfile</span>=<span class="string">&quot;$&#123;tar.destfile&#125;&quot;</span></span></span><br><span class="line"><span class="tag">                                             <span class="attr">includes</span>=<span class="string">&quot;$&#123;archive.includes&#125;&quot;</span>/&gt;</span></span><br><span class="line">                                        <span class="tag">&lt;<span class="name">gzip</span> <span class="attr">src</span>=<span class="string">&quot;$&#123;tar.destfile&#125;&quot;</span> <span class="attr">destfile</span>=<span class="string">&quot;$&#123;tar.destfile&#125;.gz&quot;</span>/&gt;</span></span><br><span class="line">                                    <span class="tag">&lt;/<span class="name">target</span>&gt;</span></span><br><span class="line">                                <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br><span class="line">                            <span class="tag">&lt;/<span class="name">execution</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;/<span class="name">executions</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">profile</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">profiles</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>最终得到的target（默认构建输出路径）目录下结构为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">├─target</span><br><span class="line">│  ├─lib		# 依赖包目录</span><br><span class="line">│  │  ├─x1.y1.z1.lib.jar</span><br><span class="line">│  │  ├─x1.y1.z1.lib.jar</span><br><span class="line">│  │  ├─...........</span><br><span class="line">│  │</span><br><span class="line">│  ├─lib-example.jar		# 源代码打包得到的thin-jar</span><br></pre></td></tr></table></figure>

<p>在linux上部署时，部署目录结构如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">├─your_deploy_dir</span><br><span class="line">│  ├─lib		# 依赖包目录</span><br><span class="line">│  │  ├─x1.y1.z1.lib.jar</span><br><span class="line">│  │  ├─x1.y1.z1.lib.jar</span><br><span class="line">│  │  ├─...........</span><br><span class="line">│  │</span><br><span class="line">│  ├─lib-example.jar		# 源代码打包得到的thin-jar</span><br><span class="line">│  │</span><br><span class="line">│  ├─arch-lib		# OS/硬件相关的依赖包的目录</span><br><span class="line">│  │  ├─zstd-jni-linux_amd64.jar</span><br><span class="line">│  │  ├─p.q.r-linux_amd64.jar</span><br></pre></td></tr></table></figure>

<p>启动时通过指定classpath和mainClass启动：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd your_deploy_dir/</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">com.connorma.example.LibExampleMain为主类类名</span></span><br><span class="line">java -classpath lib-example.jar:./lib/*:./arch-lib/* com.connorma.example.LibExampleMain</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/java/" rel="tag"># java</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/9a904cd8.html" rel="prev" title="java动态代理：java-Proxy和CGLIB">
      <i class="fa fa-chevron-left"></i> java动态代理：java-Proxy和CGLIB
    </a></div>
      <div class="post-nav-item">
    <a href="/8062c06c.html" rel="next" title="Dockerfile-CMD与ENTRYPOINT简记">
      Dockerfile-CMD与ENTRYPOINT简记 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BE%9D%E8%B5%96%E6%96%87%E4%BB%B6%E5%88%86%E7%A6%BB%E6%89%93%E5%8C%85%E5%9C%A8%E5%87%A0%E7%A7%8D%E5%9C%BA%E6%99%AF%E4%B8%8B%E7%9A%84%E4%BC%98%E5%8C%96"><span class="nav-number">1.</span> <span class="nav-text">依赖文件分离打包在几种场景下的优化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A8%B3%E5%AE%9A%E7%89%88%E6%9C%AC%E4%BE%9D%E8%B5%96%E5%8C%85%E7%9A%84%E5%88%86%E7%A6%BB%E5%AF%B9%E9%83%A8%E7%BD%B2%E7%9A%84%E4%BC%98%E5%8C%96"><span class="nav-number">1.1.</span> <span class="nav-text">稳定版本依赖包的分离对部署的优化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8D%E5%BE%97%E4%B8%8D%E4%BD%BF%E7%94%A8%E6%8C%87%E5%AE%9Aclasspath%E7%9A%84%E6%83%85%E5%86%B5%E2%80%94%E2%80%94%E4%BE%9D%E8%B5%96%E5%8C%85%E9%9D%A2%E5%90%91%E4%B8%8D%E5%90%8COS-x2F-%E7%A1%AC%E4%BB%B6%E6%9E%B6%E6%9E%84"><span class="nav-number">1.2.</span> <span class="nav-text">不得不使用指定classpath的情况——依赖包面向不同OS&#x2F;硬件架构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%A6%E4%B8%80%E7%A7%8D%E6%83%85%E5%86%B5%EF%BC%9A%E4%BD%BF%E7%94%A8classpath%E5%BC%95%E5%85%A5%E7%BA%BF%E4%B8%8A%E4%BE%9D%E8%B5%96%E5%8C%85"><span class="nav-number">1.3.</span> <span class="nav-text">另一种情况：使用classpath引入线上依赖包</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BE%9D%E8%B5%96%E6%96%87%E4%BB%B6%E5%88%86%E7%A6%BB%E6%89%93%E5%8C%85%E7%A4%BA%E4%BE%8B"><span class="nav-number">2.</span> <span class="nav-text">依赖文件分离打包示例</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">马天</p>
  <div class="site-description" itemprop="description">程序猿</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">41</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">25</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/matian2014" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;matian2014" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:matian1103@qq.com" title="E-Mail → mailto:matian1103@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">陕ICP备2023001563号 </a>
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">马天</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
